﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using VietTrips.Domain.Contexts;
using VietTrips.Domain.Interfaces;
using VietTrips.Domain.Models;

namespace VietTrips.Domain.Repositories
{
    class PlaceRepository : IPlaceRepository
    {
        readonly EntitiesContexts _contexts = new EntitiesContexts();

        public void Dispose()
        {
            _contexts.Dispose();
        }

        public IQueryable<Place> All
        {
            get { return _contexts.Places.Where(p => !p.IsDelete); }
        }

        public IQueryable<Place> AllIncluding(params Expression<Func<Place, object>>[] includeExpressions)
        {
            return null;
        }

        public Place Find(int id)
        {
            return _contexts.Places.Find(id);
        }

        public void Insert(Place place)
        {
            _contexts.Places.Add(place);
            Save();
        }

        public void Update(Place place)
        {
            _contexts.Entry(place).State = EntityState.Modified;
            Save();
        }

        public void Delete(int id)
        {
            var place = _contexts.Places.Find(id);
            if (place != null)
            {
                place.IsDelete = true;
                place.LastModified = DateTime.Now;
                Update(place);
            }
        }

        public void Save()
        {
            _contexts.SaveChanges();
        }
    }

    interface IPlaceRepository : IBaseRepository<Place>
    {
    }
}
